/*
* 报废审批
* */

import 'package:center_beijing/api/component_api.dart';
import 'package:center_beijing/api/task_api.dart';
import 'package:center_beijing/common/notification.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/widgets/custom_loading_widget.dart';
import 'package:center_beijing/widgets/imagesWatch_widget2.dart';
import 'package:center_beijing/widgets/textarea_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class ScrapApproval extends StatefulWidget {
  @override
  _ScrapApprovalState createState() => _ScrapApprovalState();
}

class _ScrapApprovalState extends State<ScrapApproval>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  CustomLoading loading = CustomLoading.withMessage('提交中...');
  CustomLoading loading2 = CustomLoading.withMessage('请稍候...');
  var scrapDetail = Map();
  var businessId = Get.arguments['businessId'];
  var taskId = Get.arguments['taskId'];
  var remark = '';

  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
    this.getScrapDetail();
  }

  @override
  void dispose() {
    _controller.dispose();
    loading.dismiss();
    loading2.dismiss();
    super.dispose();
  }

  /*
  * 获取报废详情
  * */
  getScrapDetail() async {
    loading2.show(delay: 30);
    var pass = {'id': businessId};
    var result = await getScrapDetailHistory(pass);
    loading2.dismiss();
    if (result != null&& result['errorMessage'] == null) {
      this.setState(() {
        scrapDetail = result;
      });
    }
  }

  submit(status) async {
    var pass = {
      'scrapStatus': status,
      'scrapInfoId': businessId,
      'taskId': taskId,
      'remark':scrapDetail['scrapCause'],
      'commont': remark,
      'operator': GlobalConfig.userInfo['name'],
      'operatorId': GlobalConfig.userInfo['userId'],
      'componentIds': [scrapDetail['elementId']],
      'fileIds': scrapDetail['attrList'].map((e){
        return e['attachmentId'];
      }).toList()
    };
    loading.show(delay: 30);
    var result = await scrapAudit(pass);
    loading.dismiss();
    if (result != null && result == '') {
      Fluttertoast.showToast(msg: '审核成功');
      bus.emit('refreshHome');
      Get.back();
    }
    // LogUtil.d(result);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('报废审批'),
        ),
        body: Container(
          color: Styles.MainBackgroundColor,
          padding: EdgeInsets.fromLTRB(
              0, 0, 0, MediaQuery.of(context).padding.bottom),
          child: SingleChildScrollView(
            child: GestureDetector(
              onTap: (){
                FocusScope.of(context).requestFocus(FocusNode());
              },
              child: Column(
                children: [
                  Container(
                    padding: EdgeInsets.fromLTRB(Styles.spacing8, Styles.spacing8,
                        Styles.spacing8, Styles.spacing8),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Container(
                              height: 26.h,
                              width: 4,
                              color: Styles.BlueColor,
                              margin: EdgeInsets.only(right: Styles.spacing8),
                            ),
                            Text(
                              '报废信息',
                              style: TextStyle(
                                  color: Styles.DetailColor,
                                  fontSize: Styles.Middle16Font),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB(
                        0, 0, 0, Styles.spacing8),
                    color: Colors.white,
                    child: Column(
                      children: [
                        Container(
                          padding: EdgeInsets.fromLTRB(
                              Styles.spacing8, 0, Styles.spacing8, 0),
                          margin: EdgeInsets.fromLTRB(0, Styles.spacing8, 0, 0),
                          child: Row(
                            children: [
                              Container(
                                child: Text(
                                  '订单项目:',
                                  style: TextStyle(
                                      color: Styles.DetailColor,
                                      fontSize: Styles.NormalFont),
                                ),
                              ),
                              Container(
                                margin: EdgeInsets.fromLTRB(
                                    Styles.spacing8, 0, Styles.spacing8, 0),
                                child: Text(
                                  scrapDetail['orderName'] != null
                                      ? scrapDetail['orderName']
                                      : '--',
                                  style: TextStyle(
                                      color: Styles.DetailColor,
                                      fontSize: Styles.NormalFont),
                                ),
                              )
                            ],
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.fromLTRB(0, Styles.spacing8, 0, 0),
                          padding: EdgeInsets.fromLTRB(
                              Styles.spacing8, 0, Styles.spacing8, 0),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Container(
                                child: Text(
                                  '构件编号:',
                                  style: TextStyle(
                                      color: Styles.DetailColor,
                                      fontSize: Styles.NormalFont),
                                ),
                              ),
                              Expanded(
                                  child: Container(
                                    margin: EdgeInsets.fromLTRB(
                                        Styles.spacing8, 0, Styles.spacing8, 0),
                                    child: Text(
                                      scrapDetail['code'] != null
                                          ? scrapDetail['code']
                                          : '--',
                                      style: TextStyle(
                                          color: Styles.DetailColor,
                                          fontSize: Styles.NormalFont),
                                    ),
                                  ))
                            ],
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.fromLTRB(0, Styles.spacing8, 0, 0),
                          padding: EdgeInsets.fromLTRB(
                              Styles.spacing8, 0, Styles.spacing8, 0),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Container(
                                child: Text(
                                  '报废原因:',
                                  style: TextStyle(
                                      color: Styles.DetailColor,
                                      fontSize: Styles.NormalFont),
                                ),
                              ),
                              Expanded(
                                  child: Container(
                                    margin: EdgeInsets.fromLTRB(
                                        Styles.spacing8, 0, Styles.spacing8, 0),
                                    child: Text(
                                      scrapDetail['scrapCause'] != null
                                          ? scrapDetail['scrapCause']
                                          : '--',
                                      style: TextStyle(
                                          color: Styles.DetailColor,
                                          fontSize: Styles.NormalFont),
                                    ),
                                  ))
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                  scrapDetail['attrList'] != null &&
                      scrapDetail['attrList'].length > 0
                      ? Container(
                    color: Colors.white,
                    padding: EdgeInsets.fromLTRB(Styles.spacing8, 0, Styles.spacing8, 0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          padding: EdgeInsets.fromLTRB(
                              0, Styles.spacing8, 0, Styles.spacing8),
                          child: Text(
                            '现场照片',
                            style: TextStyle(
                                color: Styles.DetailColor,
                                fontSize: Styles.NormalFont),
                          ),
                        ),
                        ImagesWatchWidget2(
                          photos:
                          scrapDetail['attrList'].map<String>((image) {
                            return "${GlobalConfig.baseUrl}fs/irest/file/mps_fileTransferHandler/image?id=${image['attachmentId']}&ZJQFSESSION=${GlobalConfig.session}";
                          }).toList(),
                        )
                      ],
                    ),
                  )
                      : Container(),

                  Container(
                    color: Colors.white,
                    margin: EdgeInsets.fromLTRB(0, Styles.spacing8, 0, 0),
                    padding: EdgeInsets.fromLTRB(
                        Styles.spacing8, 0, Styles.spacing8, 0),
                    child: TextAreaWidget(
                      labelText: '审批意见',
                      defaultValue: '',
                      placeholder: '请输入审批意见',
                      disabled: false,
                      required: false,
                      rows: 4,
                      onChange: (value) => this.setState(() {
                        remark = value;
                      }),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: Container(
          color: Colors.white,
          padding: EdgeInsets.fromLTRB(
              Styles.spacing8, Styles.spacing8, Styles.spacing8, 10),
          child: Row(
            children: [
              Expanded(
                  flex: 1,
                  child: Container(
                    decoration: new BoxDecoration(
                      border: Border.all(width: 1, color: Styles.BlueColor),
                      //背景
                      color: Styles.LightBlueColor,
                      //设置四周圆角 角度
                      borderRadius: BorderRadius.all(Radius.circular(12.0)),
                    ),
                    margin: EdgeInsets.fromLTRB(
                        Styles.spacing8, 0, Styles.spacing8, 0),
                    child: FlatButton(
                        onPressed: () {
                          FocusScope.of(context).requestFocus(new FocusNode());
                          showDialog(
                              barrierDismissible: false,
                              context: this.context,
                              builder: (BuildContext context) {
                                return AlertDialog(
                                  title: Text('注意'),
                                  content: Text('确认同意吗？'),
                                  actions: <Widget>[
                                    new Container(
                                      child: FlatButton(
                                        child: new Text(
                                          "取消",
                                          style: TextStyle(color: Colors.black),
                                        ),
                                        onPressed: () {
                                          Navigator.pop(context);
                                        },
                                      ),
                                    ),
                                    new Container(
                                      child: FlatButton(
                                        child: new Text(
                                          "确定",
                                          style: TextStyle(color: Colors.red),
                                        ),
                                        onPressed: () {
                                          Navigator.pop(context);
                                          this.submit('1');
                                        },
                                      ),
                                    ),
                                  ],
                                );
                              });
                        },
                        child: Container(
                          height: Styles.MainButtonHeight,
                          alignment: Alignment.center,
                          child: Text('同意报废',
                              style: TextStyle(
                                  color: Styles.BlueColor,
                                  fontSize: Styles.MainButtonTextFont)),
                        )),
                  )),
              Expanded(
                  flex: 1,
                  child: Container(
                    decoration: new BoxDecoration(
                      border: Border.all(width: 1, color: Styles.RedColor),
                      //背景
                      color: Colors.white,
                      //设置四周圆角 角度
                      borderRadius: BorderRadius.all(Radius.circular(12.0)),
                    ),
                    margin: EdgeInsets.fromLTRB(
                        Styles.spacing8, 0, Styles.spacing8, 0),
                    child: FlatButton(
                        onPressed: () {
                          FocusScope.of(context).requestFocus(new FocusNode());
                          showDialog(
                              barrierDismissible: false,
                              context: this.context,
                              builder: (BuildContext context) {
                                return AlertDialog(
                                  title: Text('注意'),
                                  content: Text('确认拒绝吗？'),
                                  actions: <Widget>[
                                    new Container(
                                      child: FlatButton(
                                        child: new Text(
                                          "取消",
                                          style: TextStyle(color: Colors.black),
                                        ),
                                        onPressed: () {
                                          Navigator.pop(context);
                                        },
                                      ),
                                    ),
                                    new Container(
                                      child: FlatButton(
                                        child: new Text(
                                          "确定",
                                          style: TextStyle(color: Colors.red),
                                        ),
                                        onPressed: () {
                                          Navigator.pop(context);
                                          this.submit('0');
                                        },
                                      ),
                                    ),
                                  ],
                                );
                              });
                        },
                        child: Container(
                          height: Styles.MainButtonHeight,
                          alignment: Alignment.center,
                          child: Text('拒绝申请',
                              style: TextStyle(
                                  color: Styles.RedColor,
                                  fontSize: Styles.MainButtonTextFont)),
                        )),
                  ))
            ],
          ),
        ));
  }
}
